package com.us.proframe.util;

import java.io.*;

/**
 * 
 * @ClassName: SerializeUtil
 * @Description: Serialize工具类
 * @author gaogang
 * @date 2016年7月12日 下午4:18:19
 *
 */
@SuppressWarnings("unchecked")
public class SerializerUtil {

	/**
	 * 序列化
	 * @param object
	 * @return
	 */
	public static byte[] serializeObj(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {
			throw new RuntimeException("序列化失败!", e);
		}
	}

	/**
	 * 反序列化
	 * @param bytes
	 * @return
	 */
	public static Object deserializeObj(byte[] bytes) {
		if (bytes == null) {
			return null;
		}
		ByteArrayInputStream bais = null;
		try {
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {
			throw new RuntimeException("反序列化失败!", e);
		}
	}

}
